# this should make a lizard walk around in some 3d matrix, collecting temperature
# the lizard walks randomly
# there is no regard for body temperature
# there is a fixed Pr(moving) = (0.5/12). this means the lizards has a 50% chance
# of moving after 2 minutes
# time steps are every 10s
# Tb is evaluated every time step

                               
#build environmental temperature array
Te_array <-function (Te)
  {
  a<-length(Te)/100
  Temp<-array(Te, dim=c(a,10,10))
  return(Temp)
  }
  

calc_Tb <- function (TEnew, Tb)
  {
  a<-TEnew-Tb
  if (a< -0.6)
    {
    newTb<-Tb-0.6
    }
    else if (a<0)
      {
      newTb<-Tb-a
      }
      else if (a<0.6)
        {
        newTb<-Tb+a
        }
        else if (a>0.6)
          {
          newTb<-Tb+0.6
          }
  return(newTb)
  }
         
           
Prmove <- function (Tb, VTMax, VTMin, TselUpper, TselLower)
  {
  if (Tb > VTMax)
    {
    probmove<-1
    return(probmove)
    }
    if (Tb < VTMin)
      {
      probmove<-1
      return(probmove)
      }
      if (Tb > TselUpper)
        {
        #this calculates Prmove with probability from a straight line interpolation
        #starting at lowest (Tsel, pr/move) and ending at highest (VTMax,1)
        probmove <- rbinom(1,1,(((1-(pr/move))/(VTMax-TselUpper))*(Tb-VTMax)+1))
        return(probmove)
        }
          if (Tb < TselLower)
            {
            #this calculates Prmove with probability from a straight line interpolation
            #starting at lowest (Tsel, pr/move) and ending at highest (VTMin,1)
            probmove<- rbinom(1,1,((((pr/move)-1)/(TselLower-VTMin))*(Tb-VTMin)+1))
            return(probmove)
            }
          else probmove <- (rbinom(1,1,(pr/move))) 
  return(probmove)
  } 
              
      
step <- function (probmove)     
  { 
  if (probmove==1)
    {
    #randomly pick a number in the moore-neighbourhood
    moore.step<-round(runif(1,min=1,max=8))
    }
  else
    {
    #new location = old location
    moore.step<-9
    #z=rbind(z,z[i,])
    } 
  return(moore.step)         
  }
 

#location is the location that the lizard moves to. It will be an array of moves
# row 1 = start.cell location
# row 2 = srart.cell location + coordinates of new move
# row 3 = row 2 + coordinates of new move
# I want to loop this recalculating Tb each loop 



# this is the function that takes a step, gets Te, works out Tb, find Pr(move) and takes another step
Lizard_move <- function (Te, TselLower=28, TselUpper=32, VTMax=39, VTMin=15, CTMax=44, CTMin=4, Pr=0.5, move=12)
{     

  #I need a lookup table to turn moore-cell coordinates into cartesian coordinates
  # when standing at 9, the moore-neighbourhood looks like:
  #   1 2 3
  #   4 9 5 
  #   6 7 8
  lookup.cell<-array(c(-1,0,1,-1,1,-1,0,1,0,1,1,1,0,0,-1,-1,-1,0),dim=c(9,2))
  # find the point where the lizard starts, to give it a starting env_temp and Tb.
  start.cell<-array(c(round(runif(2,min=0,max=10))),dim=c(1,2))
  location<-array(c(1,start.cell[,1],start.cell[,2]),dim=c(1,3,1))
  Temp<-Te_array(Te)
  Tenv<-c(Temp[location[1],location[2],location[3]])
  Tb<-Tenv
  #Tb starts at environmental temperature
  

  while (dim(location)[1]<dim(Temp)[1])
    {    
    for (i in 1:60)
      {
      Prob.move<-Prmove(Tb[i], VTMax, VTMin, TselUpper, TselLower)
      step.move<-step(Prob.move)
      location<-rbind(location, tail(location, n=1)+(c(0,c(lookup.cell[step.move,]))))
      Tenv<- c(Tenv, Temp[location[length(location)/3],location[length(location)*2/3],location[length(location)]])
      Tbnew<- calc_Tb(Tenv[i],Tb[i])
      Tb<-c(Tb,Tbnew)
      }
      #want to keep (location, Tenv, Tb)
    location<-rbind(location, tail(location,n=1)+(c(1,0,0)))
    }
      
      
#end of lizard_move
}      
              